/* 
 * SearchRoomManager.java 02.12.2011
 *  
 *  
 * Candidate: Alessandra Otterbach
 * Prometric ID: Algisina1
 * Username: Algisina
 *  
 * Sun Certified Developer for Java 2 Platform, Standard Edition Programming 
 * Assignment (CX-310-252A)
 *  
 * This class is part of the Programming Assignment of the Sun Certified 
 * Developer for Java 2 Platform, Standard Edition certification program, must 
 * not be used out of this context and must be used exclusively by Sun 
 * Microsystems, Inc. 
 */
package suncertify.bs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import suncertify.dao.EnumSearchCriteria;
import suncertify.dao.RoomRecord;
import suncertify.db.DB;
import suncertify.db.RecordNotFoundException;
import suncertify.util.RoomConverter;
import suncertify.util.SearchCriteriaConverter;
import suncertify.util.StringUtils;

/**
 * Class to do the search mechanism.
 * 
 * @author Alessandra Otterbach
 * 
 */
public class SearchRoomManager {

    /**
     * Searches for rooms in the database using the given criterias.
     * 
     * @param database
     * @param searchCriterias
     * @return List or RoomRecords
     * @throws RecordNotFoundException
     */
    public List<RoomRecord> searchRoom(DB database,
	    Map<EnumSearchCriteria, String> searchCriterias)
	    throws RecordNotFoundException {
	List<RoomRecord> allRooms = new ArrayList<RoomRecord>();

	int[] recNos = database.find(SearchCriteriaConverter
		.searchCriteriaAsArray(searchCriterias));

	for (int i = 0; i < recNos.length; i++) {
	    String[] recordValues = database.read(recNos[i]);
	    RoomRecord room = RoomConverter.arrayAsRoom(recordValues);
	    room.setId(recNos[i]);
	    allRooms.add(room);
	}

	// Get name and location for filtering
	String nameCriteria = null;
	String locationCriteria = null;
	if (searchCriterias != null) {
	    if (searchCriterias.get(EnumSearchCriteria.NAME) != null) {
		nameCriteria = searchCriterias.get(EnumSearchCriteria.NAME);
	    }
	    if (searchCriterias.get(EnumSearchCriteria.LOCATION) != null) {
		locationCriteria = searchCriterias
			.get(EnumSearchCriteria.LOCATION);
	    }
	}
	List<RoomRecord> toDelete = filteringSearchResult(allRooms,
		nameCriteria, locationCriteria);

	allRooms.removeAll(toDelete);

	return allRooms;
    }

    /**
     * Filtering - Implementing the "and" if name and location are given search
     * criterias.
     * 
     * @param allRooms
     * @param nameCriteria
     * @param locationCriteria
     * @return List of filtered RoomRecords.
     */
    private List<RoomRecord> filteringSearchResult(List<RoomRecord> allRooms,
	    String nameCriteria, String locationCriteria) {

	List<RoomRecord> toDelete = new ArrayList<RoomRecord>();
	for (int i = 0; i < allRooms.size(); i++) {

	    RoomRecord room = allRooms.get(i);

	    if (StringUtils.isNotEmpty(nameCriteria)
		    && StringUtils.isNotEmpty(locationCriteria)) {
		if (!(room.getName().equals(nameCriteria) && room.getLocation()
			.equals(locationCriteria))) {
		    toDelete.add(room);
		}
	    }
	}

	return toDelete;
    }
}
